package baoxin.com.robotcoach.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import baoxin.com.robotcoach.R;
import baoxin.com.robotcoach.bean.Video;
import baoxin.com.robotcoach.interfaces.VideoClick;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder> {

    List<Video> videoList;
    VideoClick listener;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View wholeView;
        ImageView thumbnail;
        TextView titile;
        TextView content;
        TextView time;

        public ViewHolder(View view) {
            super(view);
            wholeView = view;
            thumbnail = view.findViewById(R.id.video_thumbnail);
            titile = view.findViewById(R.id.video_title);
            content = view.findViewById(R.id.video_content);
            time = view.findViewById(R.id.video_time);
        }
    }

    public void setListener(VideoClick listener) {
        this.listener = listener;
    }

    public VideoAdapter(List<Video> videoList) {
        this.videoList = videoList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item, parent, false);
        View view2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item, null);
        final ViewHolder holder = new ViewHolder(view);
        holder.wholeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                listener.play(videoList.get(position));
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Video video = videoList.get(position);

        holder.thumbnail.setBackground(video.getThumbnail());
        holder.titile.setText(video.getTitle());
        holder.content.setText(video.getContent());
        holder.time.setText(video.getTime());
    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }


}
